package com.segi.view.calendar.numberpicker;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;

import com.segi.view.R;

import java.util.Calendar;

/**
 * 自定义日历控件，目前支持 年月 2016-12 年 2016 两种格式 
 * 年月日格式暂时不支持
 * 
 * @author Administrator
 * 
 */
public class CustomNumPickerDialog extends Dialog implements android.view.View.OnClickListener{
    private NumPickerListener mNumPickerListener;
    private CustomNumberPickerView yearPickerView,monthPickerView,dayPickerView;


    private int pickType = PickerEnum.YYYYMM.value();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.custom_num_picker);
		findViewById(R.id.cancel_btn).setOnClickListener(this);
		findViewById(R.id.ok_btn).setOnClickListener(this);
		yearPickerView = (CustomNumberPickerView) findViewById(R.id.num_picker_year);
		monthPickerView = (CustomNumberPickerView) findViewById(R.id.num_picker_month);
		dayPickerView = (CustomNumberPickerView) findViewById(R.id.num_picker_day);
		initPickerView();
	}
	
	private void initPickerView(){
		Calendar calendar = Calendar.getInstance();
		int month = calendar.get(Calendar.MONTH);
		if(PickerEnum.DD.value()==pickType){
			yearPickerView.setVisibility(View.GONE);
			monthPickerView.setVisibility(View.GONE);
		}else if(PickerEnum.YYYYMM.value()==pickType){
			dayPickerView.setVisibility(View.GONE);
			yearPickerView.setMinValue(2013);
			yearPickerView.setMaxValue(2050);
			yearPickerView.setValue(calendar.get(Calendar.YEAR));
			monthPickerView.setMinValue(1);
			monthPickerView.setMaxValue(12);
			monthPickerView.setValue(month+1);
		}else if(PickerEnum.YYYYMMDD.value()==pickType){
			
		}else if(PickerEnum.YYYY.value()==pickType){
			monthPickerView.setVisibility(View.GONE);
			dayPickerView.setVisibility(View.GONE);
			yearPickerView.setMinValue(2013);
			yearPickerView.setMaxValue(2050);
			yearPickerView.setValue(calendar.get(Calendar.YEAR));
		}else{//默认返回
			dayPickerView.setVisibility(View.GONE);
			yearPickerView.setMinValue(calendar.get(Calendar.YEAR));
			yearPickerView.setMaxValue(2050);
			yearPickerView.setValue(calendar.get(Calendar.YEAR));
			monthPickerView.setMinValue(1);
			monthPickerView.setMaxValue(12);
			monthPickerView.setValue(month+1);
		}
	}

	public CustomNumPickerDialog(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}
	/**
	 * 默认返回年月格式YYYYMM的日期
	 * @param context
	 * @param theme
	 * @see PickerEnum
	 */
	public CustomNumPickerDialog(Context context, int theme) {
		super(context, theme);
	}
	/**
	 * 目前暂时支持 年月格式YYYYMM   和 年格式 YYYY 2种格式的日期
	 * @param context
	 * @param theme
	 * @param pickType 
	 * @see PickerEnum
	 */
	public CustomNumPickerDialog(Context context, int theme,int pickType) {
		super(context, theme);
		this.pickType = pickType;
	}
	
	/**
	 * 目前暂时支持 年月格式YYYYMM   和 年格式 YYYY 2种格式的日期
	 * @param context
	 * @param theme
	 * @param pickType 
	 * @see PickerEnum
	 */
	public CustomNumPickerDialog(Context context, int theme,int pickType,NumPickerListener mNumPickerListener) {
		super(context, theme);
		this.pickType = pickType;
		this.mNumPickerListener = mNumPickerListener;
	}
	/**
	 * 确定按钮的回调
	 * @author Administrator
	 *
	 */
	public interface NumPickerListener{
		/**
		 * 确定按钮的回调
		 * @param year 
		 * @param month
		 * @param day
		 */
		public void sBtnCallBack(String year,String month,String day);
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(v.getId()==R.id.cancel_btn){
			dismiss();
		}else if(v.getId()==R.id.ok_btn){
			dismiss();
			if(null!=mNumPickerListener){
				if(PickerEnum.DD.value()==pickType){
					mNumPickerListener.sBtnCallBack("","",dayPickerView.getContentByCurrValue());
				}else if(PickerEnum.YYYYMM.value()==pickType){
					mNumPickerListener.sBtnCallBack(yearPickerView.getContentByCurrValue(),monthPickerView.getContentByCurrValue(),"");
				}else if(PickerEnum.YYYYMMDD.value()==pickType){
					mNumPickerListener.sBtnCallBack(yearPickerView.getContentByCurrValue(),monthPickerView.getContentByCurrValue(),dayPickerView.getContentByCurrValue());
				}else if(PickerEnum.YYYY.value()==pickType){
					mNumPickerListener.sBtnCallBack(yearPickerView.getContentByCurrValue(),"","");
				}
			}
		
		}
	}
	/**
	 * 获取选中的日期值
	 * @param pickType
	 * @return
	 */
	public String getPickerValue(int pickType){
		String selectDate="";
		if(PickerEnum.DD.value()==pickType){
			selectDate = dayPickerView.getContentByCurrValue();
		}else if(PickerEnum.YYYYMM.value()==pickType){
			selectDate = yearPickerView.getContentByCurrValue()+monthPickerView.getContentByCurrValue();
		}else if(PickerEnum.YYYYMMDD.value()==pickType){
			selectDate = yearPickerView.getContentByCurrValue()+monthPickerView.getContentByCurrValue()+dayPickerView.getContentByCurrValue();;
		}else if(PickerEnum.YYYY.value()==pickType){
			selectDate = yearPickerView.getContentByCurrValue();
		}
		return selectDate;
	}
	
	public void setYear(int year){
		if(null!=yearPickerView){
			yearPickerView.setValue(year);
		}
	}
	public void setMonth(int month){
		if(null!=monthPickerView){
			monthPickerView.setValue(month);
		}
	}
	public void setDay(int day){
		if(null!=dayPickerView){
			dayPickerView.setValue(day);
		}
	}
}
